using FytSoa.Application.Hr.Param;
using FytSoa.Domain.Hr;
using FytSoa.Sugar;
using FytSoa.Common.Utils;
using FytSoa.Common.Param;
using FytSoa.Common.Result;
using Mapster;
using Microsoft.AspNetCore.Mvc;
using SqlSugar;

namespace FytSoa.Application.Hr;

/// <summary>
/// 排班-全局服务接口
/// </summary>
[ApiExplorerSettings(GroupName = "v8")]
public class HrGlobalWorkService : IApplicationService 
{
    private readonly SugarRepository<HrGlobalWork> _thisRepository;
    public HrGlobalWorkService(SugarRepository<HrGlobalWork> thisRepository)
    {
        _thisRepository = thisRepository;
    }
    
    /// <summary>
    /// 查询所有——分页
    /// </summary>
    /// <param name="param"></param>
    /// <returns></returns>
    public async Task<PageResult<HrGlobalWorkDto>> GetPagesAsync(PageParam param)
    {
        var query = await _thisRepository.AsQueryable()
            .OrderByDescending(m=>m.Id)
            .ToPageAsync(param.Page, param.Limit);
        return query.Adapt<PageResult<HrGlobalWorkDto>>();
    }

    /// <summary>
    /// 根据用户查询全局设置内容
    /// </summary>
    /// <param name="id"></param>
    /// <returns></returns>
    [HttpGet("{id}")]
    public async Task<HrGlobalWorkDto> GetAsync(long id)
    {
        var model = await _thisRepository.AsQueryable()
            .Includes(m => m.ShiftsObj)
            .Includes(m=>m.EmployeeObj)
            .FirstAsync(m => m.EmployeeId == id);
        return model.Adapt<HrGlobalWorkDto>();
    }

    /// <summary>
    /// 添加
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> AddAsync(HrGlobalWorkAdd model)
    {
        var list = new List<HrGlobalWork>();
        foreach (var item in model.EmployeeId)
        {
            list.Add(new HrGlobalWork()
            {
                EmployeeId = item,
                ShiftsId = model.ShiftsId
            });
        }
        await _thisRepository.DeleteAsync(m => model.EmployeeId.Contains(m.EmployeeId));
        return await _thisRepository.InsertRangeAsync(list);
    }

    /// <summary>
    /// 修改
    /// </summary>
    /// <param name="model"></param>
    /// <returns></returns>
    public async Task<bool> ModifyAsync(HrGlobalWorkDto model) =>
        await _thisRepository.UpdateAsync(model.Adapt<HrGlobalWork>());

    /// <summary>
    /// 删除,支持批量
    /// </summary>
    /// <param name="ids"></param>
    /// <returns></returns>
    [HttpDelete]
    public async Task<bool> DeleteAsync([FromBody]List<long> ids) =>
        await _thisRepository.DeleteAsync(m=>ids.Contains(m.Id));
}
